/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.view;

/**
 *
 * @author David
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import org.colswe.solestac.control.AdministrationControl;
import org.colswe.solestac.entity.ActualUser;
import org.colswe.solestac.VO.UserVO;

/**
 * @version 1.0 11/09/98
 */
public class ButtonEditor extends DefaultCellEditor {
  protected JButton button;
  private String    label;
  private boolean   isPushed;
  private UserVO user = null;
  private long ides[];
  private JTable table;
  private Object[][] data;
  ActualUser actualUser = ActualUser.getInstance();

  public ButtonEditor(JCheckBox checkBox, JTable table, long[] ids, Object[][] data) {
    super(checkBox);
    this.table=table;
    this.ides=ids;
    this.data= data;
    button = new JButton();
    button.setOpaque(true);
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        fireEditingStopped();
      }
    });
  }

  public Component getTableCellEditorComponent(JTable table, Object value,
                   boolean isSelected, int row, int column) {
    if (isSelected) {
      button.setForeground(table.getSelectionForeground());
      button.setBackground(table.getSelectionBackground());
    } else{
      button.setForeground(table.getForeground());
      button.setBackground(table.getBackground());
    }
    label = (value ==null) ? "" : value.toString();
    button.setText( label );
    isPushed = true;
    return button;
  }

  public Object getCellEditorValue() {
    if (isPushed)  {
      int i = table.getSelectedRow();
      long id = ides[i];
      String name = (String) data[i][0];
      String document= (String) data[i][1];
      String role= (String) data[i][2];
      
      user = new UserVO();
      user.setId(id);
      user.setUsername(name);
      user.setDocument(document);
      user.setRole(role);
      Principal principal = actualUser.getPrincipal();
      principal.modifyUser(user);
    }
    isPushed = false;
    return new String( label ) ;
  }
  
  public boolean stopCellEditing() {
    isPushed = false;
    return super.stopCellEditing();
  }

  protected void fireEditingStopped() {
    super.fireEditingStopped();
  }
}
